/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-10-10 16:25:31
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-10-19 13:54:49
 * @FilePath: \实训二——宏业找房\egg\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const { Controller } = require('egg');
class OrdersController extends Controller {
    // 获取订单数据
    async getOrders() {
        const { ctx } = this;
        const results = await this.app.mysql.select('orders');
        const { value, status, pageCode, pageSize } = ctx.request.body;
        let newLists = results;
        console.log(value)
        if (value) {
            newLists = newLists.filter(v => v.name.includes(value) || v.tel.includes(value) || v.number.includes(value))
        }
        if (status) {
            newLists = newLists.filter(v => v.type.includes(status))
        }
        let total = newLists.length;
        newLists = newLists.slice((pageCode * 1 - 1) * pageSize, pageCode * pageSize)
        if (results) {
            ctx.body = {
                code: 200,
                list: newLists,
                total,
                msg: '获取成功'
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '获取失败'
            }
        }
    }
    // 删除数据
    async delOrders() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('orders', { id });
        if (result) {
            const results = await this.app.mysql.select('orders');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list: results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '删除失败'
            }
        }

    }

    

}

module.exports = OrdersController;
